﻿using System;
using UnityEngine;

namespace Const.util
{
    public class Utils : MonoBehaviour
    {
        /**
         * 获取2维向量的切线（法向量）
         */
        public static Vector2 GetTangent(Vector2 a)
        {
            var x = (float) (a.x * Math.Cos(Math.PI / 2) - a.y * Math.Sin(Math.PI / 2));
            var y = (float) (a.x * Math.Sin(Math.PI / 2) + a.y * Math.Cos(Math.PI / 2));
            return new Vector2(x, y);
        }


        /**
         * 获取两个向量方向的角度
         */
        public static float Angle(Vector3 from_, Vector3 to_)
        {
            //两点的x、y值
            float x = from_.x - to_.x;
            float y = from_.y - to_.y;

            //斜边长度
            float hypotenuse = Mathf.Sqrt(Mathf.Pow(x, 2f) + Mathf.Pow(y, 2f));

            //求出弧度
            float cos = x / hypotenuse;
            float radian = Mathf.Acos(cos);

            //用弧度算出角度    
            float angle = 180 / (Mathf.PI / radian);

            if (y < 0)
            {
                angle = -angle;
            }
            else if ((y == 0) && (x < 0))
            {
                angle = 180;
            }

            return angle;
        }
    }
}